package chat.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;

import java.util.ArrayList;
import java.util.List;

import chat.model.ChatModel;
import model.Message;

/**
 * 消息界面
 * Created by Administrator on 2016/3/29.
 */
public class MessageAdapter extends BaseAdapter {

    ChatModel chatModel = new ChatModel();
    List<Message> messages = new ArrayList<Message>();

    public void setChatModel(ChatModel chatModel) {
        this.chatModel = chatModel;
    }

    public void setMessages(List<Message> messages) {
        this.messages = messages;
    }

    @Override
    public int getCount() {
        return messages.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(MyApplication.share(), R.layout.item_message, null);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.id_tv_time);
            viewHolder.img_l = (ImageView) convertView.findViewById(R.id.id_img_send_l);
            viewHolder.img_r = (ImageView) convertView.findViewById(R.id.id_img_send_r);
            viewHolder.tv_msg_l = (TextView) convertView.findViewById(R.id.id_tv_message_l);
            viewHolder.tv_msg_r = (TextView) convertView.findViewById(R.id.id_tv_message_r);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Message message = messages.get(position);
        gone(viewHolder);
        viewHolder.tv_time.setText(message.getMessage_Time());
        viewHolder.tv_msg_r.setText(message.getMessage_Content());
        viewHolder.tv_msg_l.setText(message.getMessage_Content());

        // 发出的消息人
        if (message.getUserfromid().equals(MyApplication.getOwnUser().getUser_ID())) {
            visible(viewHolder.tv_msg_r);
        } else {
            visible(viewHolder.tv_msg_l);
        }

        return convertView;
    }


    class ViewHolder {
        public TextView tv_time;
        public TextView tv_msg_r;
        public TextView tv_msg_l;
        public ImageView img_r;
        public ImageView img_l;

    }

    public void gone(ViewHolder viewHolder)
    {
        viewHolder.tv_msg_r.setVisibility(View.GONE);
        viewHolder.tv_msg_l.setVisibility(View.GONE);
        viewHolder.img_l.setVisibility(View.GONE);
        viewHolder.img_r.setVisibility(View.GONE);
    }

    public void visible(View ... views)
    {
        for(View v:views)
        {
            v.setVisibility(View.VISIBLE);
        }
    }

}
